<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>28.15. fpectl — Floating point exception control &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="28. Python Runtime Services" href="python.html" />
    <link rel="next" title="29. Custom Python Interpreters" href="custominterp.html" />
    <link rel="prev" title="28.14. user — User-specific configuration hook" href="user.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="custominterp.html" title="29. Custom Python Interpreters"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="user.html" title="28.14. user — User-specific configuration hook"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="python.html" accesskey="U">28. Python Runtime Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-fpectl">
<h1>28.15. <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> &#8212; Floating point exception control<a class="headerlink" href="#module-fpectl" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module is not built by default, and its usage is discouraged
and may be dangerous except in the hands of experts.  See also the section
<a class="reference internal" href="#fpectl-limitations"><em>Limitations and other considerations</em></a> on limitations for more details.</p>
</div>
<p id="index-322">Most computers carry out floating point operations in conformance with the
so-called IEEE-754 standard. On any real computer, some floating point
operations produce results that cannot be expressed as a normal floating point
value. For example, try</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mf">1000</span><span class="p">)</span>
<span class="go">inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mf">1000</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mf">1000</span><span class="p">)</span>
<span class="go">nan</span>
</pre></div>
</div>
<p>(The example above will work on many platforms. DEC Alpha may be one exception.)
&#8220;Inf&#8221; is a special, non-numeric value in IEEE-754 that stands for &#8220;infinity&#8221;,
and &#8220;nan&#8221; means &#8220;not a number.&#8221; Note that, other than the non-numeric results,
nothing special happened when you asked Python to carry out those calculations.
That is in fact the default behaviour prescribed in the IEEE-754 standard, and
if it works for you, stop reading now.</p>
<p>In some circumstances, it would be better to raise an exception and stop
processing at the point where the faulty operation was attempted. The
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module is for use in that situation. It provides control over
floating point units from several hardware manufacturers, allowing the user to
turn on the generation of <tt class="xref docutils literal"><span class="pre">SIGFPE</span></tt> whenever any of the IEEE-754
exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem
with a pair of wrapper macros that are inserted into the C code comprising your
python system, <tt class="xref docutils literal"><span class="pre">SIGFPE</span></tt> is trapped and converted into the Python
<a title="fpectl.FloatingPointError" class="reference internal" href="#fpectl.FloatingPointError"><tt class="xref docutils literal"><span class="pre">FloatingPointError</span></tt></a> exception.</p>
<p>The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module defines the following functions and may raise the given
exception:</p>
<dl class="function">
<dt id="fpectl.turnon_sigfpe">
<tt class="descclassname">fpectl.</tt><tt class="descname">turnon_sigfpe</tt><big>(</big><big>)</big><a class="headerlink" href="#fpectl.turnon_sigfpe" title="Permalink to this definition">¶</a></dt>
<dd>Turn on the generation of <tt class="xref docutils literal"><span class="pre">SIGFPE</span></tt>, and set up an appropriate signal
handler.</dd></dl>

<dl class="function">
<dt id="fpectl.turnoff_sigfpe">
<tt class="descclassname">fpectl.</tt><tt class="descname">turnoff_sigfpe</tt><big>(</big><big>)</big><a class="headerlink" href="#fpectl.turnoff_sigfpe" title="Permalink to this definition">¶</a></dt>
<dd>Reset default handling of floating point exceptions.</dd></dl>

<dl class="exception">
<dt id="fpectl.FloatingPointError">
<em class="property">
exception </em><tt class="descclassname">fpectl.</tt><tt class="descname">FloatingPointError</tt><a class="headerlink" href="#fpectl.FloatingPointError" title="Permalink to this definition">¶</a></dt>
<dd>After <a title="fpectl.turnon_sigfpe" class="reference internal" href="#fpectl.turnon_sigfpe"><tt class="xref docutils literal"><span class="pre">turnon_sigfpe()</span></tt></a> has been executed, a floating point operation that
raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid
operation will in turn raise this standard Python exception.</dd></dl>

<div class="section" id="example">
<span id="fpectl-example"></span><h2>28.15.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates how to start up and test operation of the
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpectl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpetest</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpectl</span><span class="o">.</span><span class="n">turnon_sigfpe</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpetest</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
<span class="go">overflow        PASS</span>
<span class="go">FloatingPointError: Overflow</span>

<span class="go">div by 0        PASS</span>
<span class="go">FloatingPointError: Division by zero</span>
<span class="go">  [ more output from test elided ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mf">1000</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">FloatingPointError</span>: <span class="n-Identifier">in math_1</span>
</pre></div>
</div>
</div>
<div class="section" id="limitations-and-other-considerations">
<span id="fpectl-limitations"></span><h2>28.15.2. Limitations and other considerations<a class="headerlink" href="#limitations-and-other-considerations" title="Permalink to this headline">¶</a></h2>
<p>Setting up a given processor to trap IEEE-754 floating point errors currently
requires custom code on a per-architecture basis. You may have to modify
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> to control your particular hardware.</p>
<p>Conversion of an IEEE-754 exception to a Python exception requires that the
wrapper macros <tt class="docutils literal"><span class="pre">PyFPE_START_PROTECT</span></tt> and <tt class="docutils literal"><span class="pre">PyFPE_END_PROTECT</span></tt> be inserted
into your code in an appropriate fashion.  Python itself has been modified to
support the <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module, but many other codes of interest to numerical
analysts have not.</p>
<p>The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module is not thread-safe.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">Some files in the source distribution may be interesting in learning more about
how this module operates. The include file <tt class="docutils literal"><span class="pre">Include/pyfpe.h</span></tt> discusses the
implementation of this module at some length. <tt class="docutils literal"><span class="pre">Modules/fpetestmodule.c</span></tt>
gives several examples of use. Many additional examples can be found in
<tt class="docutils literal"><span class="pre">Objects/floatobject.c</span></tt>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">28.15. <tt class="docutils literal"><span class="pre">fpectl</span></tt> &#8212; Floating point exception control</a><ul>
<li><a class="reference external" href="#example">28.15.1. Example</a></li>
<li><a class="reference external" href="#limitations-and-other-considerations">28.15.2. Limitations and other considerations</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="user.html"
                                  title="previous chapter">28.14. <tt class="docutils literal"><span class="pre">user</span></tt> &#8212; User-specific configuration hook</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="custominterp.html"
                                  title="next chapter">29. Custom Python Interpreters</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/fpectl.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="custominterp.html" title="29. Custom Python Interpreters"
             >next</a> |</li>
        <li class="right" >
          <a href="user.html" title="28.14. user — User-specific configuration hook"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="python.html" >28. Python Runtime Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>